package vn.allmobile.ambnews.activities.list;

import java.util.List;

import vn.allmobile.ambnews.R;
import vn.allmobile.ambnews.data.Category;
import vn.allmobile.ambnews.data.Paper;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class AllPapersAdapter extends BaseExpandableListAdapter {
	List<Paper> mPapers;

	public List<Paper> getPapers() {
		return mPapers;
	}

	Context mContext;

	public AllPapersAdapter(Context context, List<Paper> listPapers) {
		this.mContext = context;
		this.mPapers = listPapers;
		// mKeysCategories = new ArrayList<List>();
		// for (int i = 0; i < this.mPapers.size(); i++) {
		// mKeysCategories.add(new ArrayList(this.mPapers.get(i)
		// .getCategories().keySet()));
		// }
	}

	@Override
	public void onGroupExpanded(int groupPosition) {
		// TODO Auto-generated method stub
		super.onGroupExpanded(groupPosition);

	}

	@Override
	public Object getChild(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		final int mGroupPosition = groupPosition;
		final int mChildPosition = childPosition;
		ChildHolder mChildHoder;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.allpapers_list_group_item, null);
			mChildHoder = new ChildHolder();
			mChildHoder.title = (TextView) convertView
					.findViewById(R.id.allpapers_item_title);
			mChildHoder.checkBox = (CheckBox) convertView
					.findViewById(R.id.allpapers_chkCheckCategory);
			convertView.setTag(mChildHoder);
		} else {
			mChildHoder = (ChildHolder) convertView.getTag();
		}
		String mTitle = this.mPapers.get(groupPosition).getCategories()
				.get(childPosition).getTitle();
		final boolean mIsEnable = this.mPapers.get(groupPosition)
				.getCategories().get(childPosition).isEnable();

		mChildHoder.title.setText(mTitle);
		mChildHoder.checkBox.setChecked(mIsEnable);
		mChildHoder.checkBox.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mPapers.get(mGroupPosition).getCategories().get(mChildPosition)
						.setEnable(!mIsEnable);
				if (mIsEnable == false) {// False => True
					mPapers.get(mGroupPosition).setEnable(true);
				} else {
					// Check others child, if all is unable => unable group
					boolean mIsAllUnable = true;
					// for (Object mKey : mPapers.get(mGroupPosition)
					// .getCategories().keySet()) {
					// if (mPapers.get(mGroupPosition).getCategories()
					// .get(mKey).isEnable()) {
					// mIsAllUnable = false;
					// break;
					// }
					// }
					for (Category category : mPapers.get(mGroupPosition)
							.getCategories()) {
						if (category.isEnable()) {
							mIsAllUnable = false;
							break;
						}
					}
					if (mIsAllUnable) {
						mPapers.get(mGroupPosition).setEnable(false);
					}
				}
				notifyDataSetChanged();
			}
		});
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return this.mPapers.get(groupPosition).getCategories().size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return mPapers.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		final int mGroupPosition = groupPosition;
		final boolean mIsExpanded = isExpanded;
		GroupHolder mGroupHoder;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.allpapers_list_group, null);
			mGroupHoder = new GroupHolder();
			mGroupHoder.image = (ImageView) convertView
					.findViewById(R.id.allpapers_tag_img);
			mGroupHoder.title = (TextView) convertView
					.findViewById(R.id.allpapers_group_title);
			mGroupHoder.checkBox = (CheckBox) convertView
					.findViewById(R.id.allpapers_chkCheckPaper);
			convertView.setTag(mGroupHoder);
		} else {
			mGroupHoder = (GroupHolder) convertView.getTag();
		}

		String mGroupTitle = this.mPapers.get(groupPosition).getTitle();
		final boolean mIsEnable = this.mPapers.get(groupPosition).isEnable();
		// Set
		mGroupHoder.title.setText(mGroupTitle);
		mGroupHoder.checkBox.setChecked(mIsEnable);
		if (isExpanded) {
			mGroupHoder.image.setImageResource(R.drawable.group_up_btn);
		} else {
			mGroupHoder.image.setImageResource(R.drawable.group_down_btn);
		}
		mGroupHoder.checkBox.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mPapers.get(mGroupPosition).setEnable(!mIsEnable);
				// Set for child
				// for (Object mKey : mKeyList) {
				// mPapers.get(mGroupPosition).getCategories().get(mKey)
				// .setEnable(!mIsEnable);
				// }
				for (Category category : mPapers.get(mGroupPosition)
						.getCategories()) {
					category.setEnable(!mIsEnable);
				}
				if (mIsExpanded) {
					notifyDataSetChanged();
				}
			}
		});
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return false;
	}

	// //////////////////////////////////////////////////////////////////////
	class GroupHolder {
		ImageView image;
		TextView title;
		CheckBox checkBox;
	}

	class ChildHolder {
		TextView title;
		CheckBox checkBox;
	}

}
